I deployed my python application with external packages/libraries to Lambda using the .zip option

I deployed my python application with external packages/libraries to Lambda using the .zip option

Clock Icon2023.04.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Even though using Serverless (SAM) is a good option to deploy applications, I didn't want to rule out the option for me to deploy my python application directly to Lambda and not miss out on the external libraries which are not in-built in Lambda.

The code for your AWS Lambda function is made up of scripts or compiled programs, as well as their dependencies. To deploy your function code to Lambda, you utilize a deployment package. Container images and .zip file archives are the two forms of deployment packages Supported by Lambda.

Runtime dependencies

To create a Lambda function with or without runtime dependencies, a deployment package is necessary. The deployment package serves as the source bundle for running the code and dependencies of the function on Lambda.

Any package, module, or other external libraries that are not included with the Lambda runtime environment for your function's code are considered dependencies.

For example, in this blog, I have written Python code but with external dependencies like the better_profanity library and requests library. I want to have these libraries in the Lambda function.

Deployment with Dependencies

For creating the deployment package with the dependencies:

I will be using an existing project ready for deployment to lambda from my vs code

cd into the project directory

cd project-directory-name

My directory structure looks like this

Install the external libraries to the package (you can give any name) directory

I have installed better_profanity and Flask library

pip install --target ./package better_profanity

pip install --target ./package flask

Create the deployment package with the installed external libraries in the root folder.

cd into the package directory, and zip all the libraries in the folder.

cd package
zip -r ../my-deployment.zip .

This generates the my-deployment.zip file in the project directory

To add the app.py file (which is the main application that you are deploying) to the root of the zip file

cd ..  //This returns you back to the root directory

zip my-deployment.zip app.py

Using the Lambda console

Will be uploading the .zip file to Lambda using the Lambda console

Open the Function page on the Lambda console.

Select a function

Choose Upload from and then .zip file.

Choose Upload to select your local .zip file

Choose Save

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.